<?php
class LiningAction extends AdminAction
{
 	public function _initialize()
	 {
	 	parent::_initialize();
	 }
	 //供应商列表
	 public function index()
	 {
	 		$post = $_REQUEST;
			$where = array();
	
		    $pageRows = 20;
			if(isset($_REQUEST['pageRows']) && intval($_REQUEST['pageRows'])){
				$pageRows = intval($_REQUEST['pageRows']);
			}
			
		 //order by
			if(isset($_REQUEST['sort_by']) && $_REQUEST['sort_by']){
				$sort_by = $_REQUEST['sort_by'];
			}else{
				$sort_by = 'DESC';
			}
			
			if(isset($_REQUEST['order_by']) && $_REQUEST['order_by']){
				$order_by = $_REQUEST['order_by'];
			}else{
				$order_by = 'lining_id';
			}

		    //搜索关键词
			if(isset($post['keyword']) && $post['keyword']){
				if($post['searchtype'] == 1){
					$where['suppliers_name'] = array('like','%'.$post['keyword'].'%');
				}else if($post['searchtype'] == 2){
					$where['lining_id'] = $post['keyword'];
				}
				$this->assign('searchtype', $post['searchtype']);
			    $this->assign('searchkeyword', $post['keyword']);
			}				
			
		 	$lining_db = D('lining');
		    $lining_db->where($where);	
		 	$count = $lining_db->count();
			import("ORG.Util.Page");
			$Page = new Page($count, $pageRows);
			$pageShow = $Page->show();
			$this->assign('pageShow', $pageShow);
			$nowPage = $Page->getNowPageUrl();//当前页url
			//如果是post过来的则跳转到带参数的url
			if($_POST){
			    redirect($nowPage);exit;
			}
			//保存当前url
			cookie('lining_list_nowPage',$nowPage);	
            
            $lining_db->where($where);
			$lining_db->limit($Page->firstRow.','.$Page->listRows);
			$lining_db->order( $order_by.' '. $sort_by);
			$liningList = $lining_db->select();

            foreach ($liningList as $key => $val) {
            	$liningList[$key]['surplus'] = SuppliersModel::inventorySurplus(1,$val['lining_id']);
            	$liningList[$key]['decreaseAmount'] = SuppliersModel::inventoryDecrease(1,$val['lining_id']);
            	$liningList[$key]['order_count'] = D('inventory')->where(array('type'=>1,'tid'=>$val['lining_id'],'order_item_id'=>array('gt',0)))->count();
            }
            
            //预警
            $surplusTips = array();
            $supplierListYJ = $lining_db->order('lining_id desc')->select();
            foreach ($supplierListYJ as $key => $val) {
            	$surplus = SuppliersModel::inventorySurplus(1,$val['lining_id']);
            	if($surplus<30){
                   $val['surplus'] = SuppliersModel::inventorySurplus(1,$val['lining_id']);
                   $surplusTips[] = $val;
            	}
            }
            $this->assign('surplusTips', $surplusTips);

			$this->assign('liningList', $liningList);
			$this->assign('sort_by', $sort_by);
			$this->assign('order_by', $order_by);
			$this->display('index');
	 }
	 
	 //添加供应商
	 public function lining_add()
	 {
	 		if($_POST['dosubmit']){
	 			$post = $_POST;
	 			$lining_id = LiningModel::updateLiningInfo($post);
	 			if($lining_id){
	 				LiningModel::updateLiningAdmin($post['admins'], $lining_id);
	 				$this->success('操作成功', U('lining/index'));
	 			}else{
	 				$this->error('操作失败', U('lining/index'));
	 			}
	 		}else{
	 			$admindb = D('admin');
	 			$adminList = $admindb->field("username,id,CASE WHEN lining_id = 0 THEN 'free' ELSE 'other' END AS 'type'")->select();
//	 			echo $admindb->getLastSql();exit;
	 			$this->assign('adminList', $adminList);
	 			$this->display('lining_form');
	 		}
	 }

	 //编辑供应商
	public function lining_edit()
	 {
	 	$lining_id = intval($_GET['sid']);
	 	$liningInfo = LiningModel::getLiningInfo($lining_id);
	 	if(!$liningInfo){
	 		$this->error('该供应商不存在', U('lining/lining_list'));
	 	}
	 		if($_POST['dosubmit']){
	 			$post = $_POST;
	 			$lining_id = LiningModel::updateLiningInfo($post, $lining_id);
	 			if($lining_id){
	 				LiningModel::updateLiningAdmin($post['admins'], $lining_id);
	 				$this->success('操作成功', U('lining/index'));
	 			}else{
	 				$this->error('操作失败', U('lining/index'));
	 			}
	 		}else{
	 			$admindb = D('admin');
	 			$adminList = $admindb->field("username,id,CASE WHEN lining_id = ".$lining_id." THEN 'this' WHEN suppliers_id = 0 THEN 'free' ELSE 'other' END AS 'type'")->select();
	 			$this->assign('liningInfo', $liningInfo);
	 			$this->assign('adminList', $adminList);
	 			$this->display('lining_form');
	 		}
	 }
	 
	 public function lining_delete()
	 {
	 	$lining_id = intval($_GET['sid']);
	 	$liningInfo = LiningModel::getLiningInfo($lining_id);
	 	if(!$lining_id){
	 		$this->error('该供应商不存在', U('lining/index'));
	 	}
	 	LiningModel::deleteLiningInfo($lining_id);
	 	$this->success('操作成功', U('lining/index'));
	 }
	 
	 //ajax验证供应商名称
	 public function lining_checkname_ajx()
	 {
	 		$lining_db = D('lining');
	 		$lining_id = intval($_GET['sid']);
	 		$lining_name = trim($_GET['suppliers_name']);
	 		
	 		if($lining_id){
	 			$where['lining_id'] = array('neq', $lining_id);
	 		}
	 	
	 		$where['suppliers_name'] = $lining_name;
	 		
	 		$count = $lining_db->where($where)->count();
	 		if($count){
	 			echo(0);
	 		}else{
	 			echo(1);
	 		}
	 
	 }

	 //手动修改里布上下架时间
	 public function edit_lining_time(){
	 	$type = $_REQUEST['type'];
        $id   = intval($_REQUEST['id']);
        $val  = $_REQUEST['val'];
        
        $editData = array();
        if($type == 'up'){
            $editData['up_time'] = strtotime($val);        	
        }else{
        	$editData['down_time'] = strtotime($val);  
        }

        echo D('lining')->where(array('lining_id'=>$id))->save($editData);          	

	 }
}